package com.fowo.api.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.R;
import com.fowo.api.sys.entity.SysPost;
import com.fowo.api.sys.model.SysPostSearchParamPo;
import com.fowo.api.sys.service.SysPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Tag(name = "职位")
@RequestMapping("/api/sys/posts")
public class SysPostController {
    @Resource
    private SysPostService sysPostService;

    @GetMapping("page")
    @Operation(summary = "分页查询")
    public R<Page<SysPost>> page(SysPostSearchParamPo search) {
        return R.data(sysPostService.pageSearch(search));
    }

    @GetMapping
    @Operation(summary = "搜索")
    public R<List<SysPost>> search(SysPostSearchParamPo search) {
        return R.data(sysPostService.search(search));
    }

    @GetMapping("{id}")
    @Operation(summary = "获取单个信息")
    public R<SysPost> getById(@PathVariable Long id) {
        return R.data(sysPostService.getById(id));
    }

    @PostMapping
    @Operation(summary = "新增职位")
//    @PreAuthorize("@am.canAccess('sysPostsCreate')")
    public R<Long> create(@RequestBody @Valid SysPost sysPost) {
        return R.data(() -> sysPostService.create(sysPost));
    }

    @PutMapping
    @Operation(summary = "更新职位")
//    @PreAuthorize("@am.canAccess('sysPostsEdit')")
    public R update(@RequestBody @Valid SysPost sysPost) {
        return R.ok(() -> sysPostService.update(sysPost));
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除职位")
//    @PreAuthorize("@am.canAccess('sysPostsDelete')")
    public R delete(@PathVariable Long id) {
        return R.ok(() -> sysPostService.delete(id));
    }
}
